package com.zhenyi.zhengfang.config;

import com.cjw.model.DBMap;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.PropKit;
import com.zhenyi.core.wechat.utils.TemplateUtils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;

public class ZFTemplateUtils extends TemplateUtils {

	private final String TEMPLATE_ID_SEND_REPLY = "W1dG26zIr6bKf8It2woJFuZqsdwYAwqCH51tNwrWomE";

	public ZFTemplateUtils() {
		super(PropKit.get("ZF_APP_ID"), PropKit.get("ZF_APP_SECRET"));
	}

	public void sendReplied(String openId, String formId, Long commentId) {
		sendTemplateMsg(openId, formId, TEMPLATE_ID_SEND_REPLY, "pages/usercomment/usercomment?commentId=" + commentId,
				"已回复", "点击进入小程序查看", DateUtil.now());
	}

	public void sendTemplateMsg(String openId, String formId, String templateId, String pagePath, String keyword1Str,
			String keyword2Str, String keyword3Str) {

		String accessToken = getAccessToken();
		DBMap map = new DBMap();

		DBMap data = new DBMap();
		DBMap keyword1 = new DBMap();
		keyword1.put("value", keyword1Str);
		DBMap keyword2 = new DBMap();
		keyword2.put("value", keyword2Str);
		DBMap keyword3 = new DBMap();
		keyword3.put("value", keyword3Str);

		data.put("keyword1", keyword1);
		data.put("keyword2", keyword2);
		data.put("keyword3", keyword3);

		map.put("touser", openId);
		map.put("template_id", templateId);
		map.put("page", pagePath);
		map.put("form_id", formId);
		map.put("data", data);

		String json = JsonKit.toJson(map);
		System.out.println(json);
		String url = TEMPLATE_URL + accessToken;
		System.out.println(HttpUtil.post(url, json));
	}

}
